<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" size="large" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="维护项" prop="itemId">
        <el-select @change="handleQuery" clearable filterable class="!w-240px" v-model="queryParams.itemId"
          placeholder="请选择设备分类">
          <el-option v-for="dict in settingTypeList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentId">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="维护状态" prop="status">
        <el-select @change="handleQuery" v-model="queryParams.status" placeholder="请选择维护状态" clearable class="!w-240px">
          <el-option clearable v-for="dict in getIntDictOptions(DICT_TYPE.KEEP_TYPE)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="责任人" prop="dutyUserName">
        <el-input v-model="queryParams.dutyUserName" placeholder="请输入责任人名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table border row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column label="维护单号" align="center" prop="keepNo" min-width="180" />
      <el-table-column label="设备名称" align="center" prop="equipmentName">
        <template #default="{ row }">
          <el-button type="success" text style="font-size: 14px;" @click="openEquipmentViewDialog(row.equipmentId)">
            {{ row.equipmentName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="设备型号" align="center" prop="equipmentModel" />
      <el-table-column label="所属部门" align="center" prop="deptName" />
      <el-table-column label="维护次数" align="center" prop="keepCount" />
      <el-table-column label="维护时长(小时)" align="center" prop="hours">
        <template #default="{ row }">
          <el-button type="success" text style="font-size: 14px;" @click="openEquipmentViewDialog(row.equipmentId)">
            {{ row.taskRespVO?.hours }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openHistoryRecords(scope.row)">
            历史维护记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>


  <!-- 维修申请单详细信息 -->
  <!-- <RepairTaskInfoView ref="viewRef" /> -->

  <!-- 设备详细信息 -->
  <EquipmentInfoView ref="equipmentView" />

  <!-- <Dialog title="维修维护记录" v-model="dialogVisibleHistory" width="70vw" :fullscreen="true">
    <historyRecords ref="historyRef" />
  </Dialog> -->

  <el-drawer v-model="dialogVisibleHistory" :with-header="false" size="70%">
    <historyRecords ref="historyRef" />
  </el-drawer>

</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import download from '@/utils/download'
import { FaultClassificationApi, FaultClassification } from '@/api/business/faultclassification'
import RepairInfoView from '../repairrequest/RepairInfoView.vue'
import EquipmentInfoView from '../equipmentmanage/EquipmentInfoView.vue'
import { RepairTaskApi, RepairTask } from '@/api/business/repairtask'
// import RepairTaskInfoView from './RepairTaskInfoView.vue'
import { dateFormatter, formatDate } from '@/utils/formatTime'
import historyRecords from './historyRecords.vue'

import { EquipmnetKeepWaitApi, EquipmnetKeepWait } from '@/api/business/equipmnetkeepwait'
import { EquipmentManageApi, EquipmentManage } from '@/api/business/equipmentmanage'
import { EquipmentKeepSettingTypeApi, EquipmentKeepSettingType } from '@/api/business/equipmentkeepsettingtype'
import { KeepTaskApi, KeepTask } from '@/api/business/keeptask'


/** 维修申请 列表 */
defineOptions({ name: 'RepairRequest' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisibleHistory = ref(false)
const historyRef = ref()

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  faultId: undefined,
  equipmentName: undefined,
  urgency: undefined,
  status: undefined,
  createTime: []
})


const queryParamsSettingType = reactive({
  pageNo: 1,
  pageSize: 100,
  keepNo: undefined,
  itemId: undefined,
  equipmentId: undefined,
  status: undefined,
  dutyUserName: undefined
})

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await KeepTaskApi.getKeepTaskStatisticsDamage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

//所有维护项
const settingTypeList = ref([])

const getSettingTypeList = async () => {
  const data = await EquipmentKeepSettingTypeApi.getEquipmentKeepSettingTypePageDamage(queryParamsSettingType);
  settingTypeList.value = data.list

}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}



/** 历史维修记录 */
const openHistoryRecords = async (row) => {
  dialogVisibleHistory.value = true
  await nextTick()
  historyRef.value.initData(row)

}

//报工按钮
const reporting = () => {

}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: RepairRequest[]) => {
  checkedIds.value = records.map((item) => item.id);
}


//初始化数据
const initData = async () => {
  getSettingTypeList()
}

//维修详细信息
const viewRef = ref()

//维修申请详细信息
const openViewDialog = (id) => {
  viewRef.value.openDetailInfo(id, true)
}

//设备详细信息
const equipmentView = ref()
//设备详细信息
const openEquipmentViewDialog = (id) => {
  equipmentView.value.openDetailInfo(id)
}


/** 初始化 **/
onMounted(() => {
  getList()
  initData()
})
</script>
